package com.novax.ex.activity.provider.service;

import com.novax.ex.activity.infrastructure.entity.RankConfig;
import com.novax.ex.activity.infrastructure.entity.RankSubConfig;
import com.novax.ex.activity.infrastructure.mapper.RankConfigMapper;
import com.novax.ex.activity.infrastructure.mapper.RankSubConfigMapper;
import com.novax.ex.activity.open.model.request.RankConfigRequest;
import com.novax.ex.common.base.BaseMapper;
import com.novax.ex.common.base.BaseService;
import com.novax.ex.common.enums.RankSubConfigType;
import com.novax.ex.common.util.CopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;

/**
 * @Description
 * @Author ChenXi 排名赛配置
 * @Date 2023/11/23/15:46
 */

@Service
public class RankConfigService extends BaseService {

    @Resource
    private RankConfigMapper rankConfigMapper;
    @Resource
    private RankSubConfigMapper rankingSubConfigMapper;

    @Override
    protected BaseMapper getBaseMapper() {
        return rankConfigMapper;
    }

    /**
     * @param rankConfigRequest
     * @Description: 新建排名赛活动配置与子活动
     * @Param: [rankConfigRequest]
     * @Author: ChenXi
     * @Date: 2023/11/24 09:44
     * @return: boolean
     **/
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean addRankConfig(RankConfigRequest rankConfigRequest) {

        RankConfig rankConfig = CopyUtils.copyObject(rankConfigRequest, RankConfig.class);
        int i = rankConfigMapper.insertSelective(rankConfig);
        if (i > 0) {
            // 新建默认三条子活动
            i += rankingSubConfigMapper.insertSelective(
                new RankSubConfig(rankConfig.getId(), RankSubConfigType.TradeVolumeRanking.getType(),
                                  RankSubConfigType.TradeVolumeRanking.getDesc(), null));
            i += rankingSubConfigMapper.insertSelective(
                new RankSubConfig(rankConfig.getId(), RankSubConfigType.profitRateRanking.getType(),
                                  RankSubConfigType.profitRateRanking.getDesc(), null));
            i += rankingSubConfigMapper.insertSelective(
                new RankSubConfig(rankConfig.getId(), RankSubConfigType.profitAmountRanking.getType(),
                                  RankSubConfigType.profitAmountRanking.getDesc(), null));
        }
        return i > 0;
    }

    /**
     * @param
     * @Description: 查询开启中的上架活动
     * @Author: ChenXi
     * @Date: 2023/11/24 11:28
     * @return: com.novax.ex.activity.infrastructure.entity.RankConfig
     **/
    public RankConfig findOpen() {
        return rankConfigMapper.findOpen();
    }

    /**
     * @param
     * @Description: 查询上架的活动 不考虑是否开始时间
     * @Author: ChenXi
     * @Date: 2023/11/24 11:28
     * @return: com.novax.ex.activity.infrastructure.entity.RankConfig
     **/
    public RankConfig findOpenWithOutTime() {
        return rankConfigMapper.findOpenWithOutTime();
    }


}

